<?php
add_action('widgets_init', 'register_slider_widget'); // load widget

function register_slider_widget() { // register widget
    register_widget('slider');
}
class slider extends WP_Widget {// widget class {

    function slider() {
    // widget setting | class name must be same function name
        $widget_ops = array('classname' => 'Lastest news-w', 'description' =>
            'Display Slider Home page');
        $control_ops = array(
            'width' => 250,
            'height' => 350,
            'id_base' => 'slider');
        $this->WP_Widget('slider', 'CouponX - Slider Home page', $widget_ops, $control_ops);
    }
    function widget($args, $instance) { // display widget
        $ids_input = $instance['ids'];
        $ids_st = explode(',', $ids_input);
        $args = array(
            'post_type' => 'store',
            'post_status' => 'publish',
            'posts_per_page' => 7,
            'post__in' => $ids_st
        );
        $query = new WP_Query($args);
        $stores = $query->posts;
        ?>
            <div id="pxs_container" class="pxs_container">
<!--			<div class="pxs_loading">Loading images...</div>-->
			<div class="pxs_slider_wrapper">
				<ul class="pxs_slider">
                <?php 
                        foreach ($stores as $st){//Begin loop stores
                            $st_id = $st->ID;
                            $title_st = $st->post_title;
                            $arg = array(
                                'post_type' => 'coupon',
                                'post_status' => 'publish',
                                'posts_per_page' => 1,
                                'meta_query' => array(
                                    'relation' => 'AND',
                                    array(
                                        'key' => 'store_coupon_metadata',
                                        'value' => $st_id,
                                        'compare' => '='
                                    )
                                )
                            );
                            $query = new WP_Query($arg);
                            $code_cp = $query->posts;
                            $total = $query->found_posts;
                            foreach ($code_cp as $cp){//Begin loop Coupon 
                                if($total >0){
                                    $cp_id = $cp->ID;
                                    $event = wp_get_post_terms( $cp_id, 'event');
                                    $cp_type = get_post_meta($cp_id,'coupon_type_metadata',true);
                                    $store_id = get_post_meta($cp_id, 'store_coupon_metadata', true);
                                    $store_name = get_the_title($store_id);
                                    $outlink_cp = '/'.get_option('aff_out_link_prefix_cp').'/'.$cp_id;
                                    ?>
                                <li id="<?php echo $cp_id;?>" class="sl data-cp" data-store="<?php echo $store_id;?>" data-current_url="<?php echo cpx_current_url();?>" data-offer_id="<?php echo $cp_id;?>">
                                    <div class="row-fluid">
                                        <div class="span6">
                                            <p class="sl-store-name"><a class="sl-store" target="_parent" title="<?php echo 'View more ' . $store_name . ' coupons'; ?>" href="<?php echo get_permalink($store_id); ?>"><?php echo $store_name;?></a></p>
                                            <a rel="nofollow" href="<?php echo $outlink_cp; ?>" target="<?php echo $store_id;?>" class="coupon_title title"><?php echo $cp->post_title;?></a>
                                            <a rel="nofollow" href="<?php echo $outlink_cp; ?>" target="<?php echo $store_id;?>" class="coupon_title code-wraper">
                                                <div class="button">
                                                    <?php 
                                                        if($cp_type == 'free_shipping' || $cp_type == 'code'){
                                                            echo 'Use Code';
                                                        }else{echo 'Get Deal';}
                                                    ?>
                                                </div>
                                            </a>
                                            <?php foreach ($event as $v){ ?>
                                                <p class="event"><a href="<?php echo get_term_link( $v, 'event' );?>">More event <?php echo $v->name;?></a></p>
                                            <?php }?> 
                                            <p class="discount">
                                                <a rel="nofollow" href="<?php echo $outlink_cp; ?>" target="<?php echo $store_id;?>" class="coupon_title"><?php echo truncate_str($cp->post_content, 150);?></a>
                                            </p>
                                            <a class="more-st" title="<?php echo 'See more '.$store_name.' coupons'; ?>" href="<?php echo get_permalink($store_id);?>">See more <?php echo $store_name;?> offers &gt;&gt;</a>
                                        </div>
                                        <div class="span6">
                                            <div class="img-parallax">
                                                <img src="<?php echo get_post_meta($st->ID,'logo_metadata',true);?>" alt="<?php echo $title_st.' coupons';?>" title="<?php echo $title_st.' coupons';?>" />
                                            </div>
                                        </div>
                                    </div>
                                        
                                </li>
                            <?php }
                                }//End loop coupon
                        }//End loop stores
                        ?>
                    </ul>
                    <div class="pxs_navigation">
			<span class="pxs_next"></span>
			<span class="pxs_prev"></span>
                    </div>
                        <ul class="pxs_thumbnails">
                                   <?php 
                                      foreach ($stores as $st){
                                          $st_id = $st->ID;
                                          $st_name = $st->post_title;
                                                ?>
                                            <li>
                                                <div class="img-nav">
                                                    <img src="<?php echo get_post_meta($st->ID,'logo_metadata',true);?>" alt="<?php echo $st_name;?>" title="<?php echo $st_name;?>" />
                                                </div>
                                            </li>
                                            
                                   <?php }?>
                                 </ul>
                  </div>
                </div>
    <?php }

    function update($new_instance, $old_instance) { // update widget
        $instance = $old_instance;
        $instance['title'] = $new_instance['title'];
        $instance['ids'] = $new_instance['ids'];
        return $instance;
    }

    function form($instance) { // form for the widget options
        ?>
        <div style="color: #333;">
            <p>
                <label for="<?php echo $this->get_field_id('count_item');?>"><strong>Enter ids store.</strong> Ex id1,id2,..</label>
    		<input type="text" id="<?php echo $this->get_field_id('ids');?>" name="<?php echo $this->get_field_name('ids');?>" value="<?php echo $instance['ids'];?>" style="width:90%;" />
            </p>
        </div>
        <?php
    }

}
?>
